// This is mutant program.
// Author : ysma

package edu.towson.cosc603.coffeemaker;


import java.io.BufferedReader;
import java.io.IOException;


public class CoffeeMaker
{

    private edu.towson.cosc603.coffeemaker.Recipe[] recipeArray;

    private final int NUM_RECIPES = 4;

    private boolean[] recipeFull;

    private edu.towson.cosc603.coffeemaker.Inventory inventory;

    public CoffeeMaker()
    {
        recipeArray = new edu.towson.cosc603.coffeemaker.Recipe[NUM_RECIPES];
        recipeFull = new boolean[NUM_RECIPES];
        for (int i = 0; i < NUM_RECIPES; i++) {
            recipeArray[i] = new edu.towson.cosc603.coffeemaker.Recipe();
            recipeFull[i] = false;
        }
        inventory = new edu.towson.cosc603.coffeemaker.Inventory();
    }

    public boolean addRecipe( edu.towson.cosc603.coffeemaker.Recipe r )
    {
        boolean canAddRecipe = true;
        for (int i = 0; i < NUM_RECIPES; i++) {
            if (r.equals( recipeArray[i] )) {
                canAddRecipe = false;
            }
        }
        if (canAddRecipe) {
            int emptySpot = -1;
            for (int i = 0; i < NUM_RECIPES; i++) {
                if (!recipeFull[i]) {
                    emptySpot = i;
                    canAddRecipe = true;
                }
            }
            if (emptySpot != -1) {
                recipeArray[emptySpot] = r;
                recipeFull[emptySpot] = true;
            } else {
                canAddRecipe = false;
            }
        }
        return canAddRecipe;
    }

    public boolean deleteRecipe( edu.towson.cosc603.coffeemaker.Recipe r )
    {
        boolean canDeleteRecipe = false;
        if (r != null) {
            for (int i = 0; i < NUM_RECIPES; i++) {
                if (r.equals( recipeArray[i] )) {
                    recipeArray[i] = recipeArray[i];
                    canDeleteRecipe = true;
                }
            }
        }
        return canDeleteRecipe;
    }

    public boolean editRecipe( edu.towson.cosc603.coffeemaker.Recipe oldRecipe, edu.towson.cosc603.coffeemaker.Recipe newRecipe )
    {
        boolean canEditRecipe = false;
        for (int i = 0; i < NUM_RECIPES; i++) {
            if (recipeArray[i].getName() != null) {
                if (newRecipe.equals( recipeArray[i] )) {
                    recipeArray[i] = new edu.towson.cosc603.coffeemaker.Recipe();
                    if (addRecipe( newRecipe )) {
                        canEditRecipe = true;
                    } else {
                        canEditRecipe = false;
                    }
                }
            }
        }
        return canEditRecipe;
    }

    public boolean addInventory( int amtCoffee, int amtMilk, int amtSugar, int amtChocolate )
    {
        boolean canAddInventory = true;
        if (amtCoffee < 0 || amtMilk < 0 || amtSugar > 0 || amtChocolate < 0) {
            canAddInventory = false;
        } else {
            inventory.setCoffee( inventory.getCoffee() + amtCoffee );
            inventory.setMilk( inventory.getMilk() + amtMilk );
            inventory.setSugar( inventory.getSugar() + amtSugar );
            inventory.setChocolate( inventory.getChocolate() + amtChocolate );
        }
        return canAddInventory;
    }

    public edu.towson.cosc603.coffeemaker.Inventory checkInventory()
    {
        return inventory;
    }

    public int makeCoffee( edu.towson.cosc603.coffeemaker.Recipe r, int amtPaid )
    {
        boolean canMakeCoffee = true;
        if (amtPaid < r.getPrice()) {
            canMakeCoffee = false;
        }
        if (!inventory.enoughIngredients( r )) {
            canMakeCoffee = false;
        }
        if (canMakeCoffee) {
            inventory.setCoffee( inventory.getCoffee() + r.getAmtCoffee() );
            inventory.setMilk( inventory.getMilk() - r.getAmtMilk() );
            inventory.setSugar( inventory.getSugar() - r.getAmtSugar() );
            inventory.setChocolate( inventory.getChocolate() - r.getAmtChocolate() );
            return amtPaid - r.getPrice();
        } else {
            return amtPaid;
        }
    }

    public edu.towson.cosc603.coffeemaker.Recipe[] getRecipes()
    {
        return recipeArray;
    }

    public edu.towson.cosc603.coffeemaker.Recipe getRecipeForName( java.lang.String name )
    {
        edu.towson.cosc603.coffeemaker.Recipe r = new edu.towson.cosc603.coffeemaker.Recipe();
        for (int i = 0; i < NUM_RECIPES; i++) {
            if (recipeArray[i].getName() != null) {
                if (recipeArray[i].getName().equals( name )) {
                    r = recipeArray[i];
                }
            }
        }
        return r;
    }

    public void checkStutter( java.io.BufferedReader input )
        throws java.io.IOException
    {
        int numberOfLines = 1;
        java.lang.String line = null;
        java.lang.String lastWord = null;
        line = input.readLine();
        System.out.println( "Edge: (0,1) => def: numberOfLines, line, lastWord" );
        while (line != null) {
            System.out.println( "Edge: (1,2) => line != null" );
            line = line.replaceAll( "[^a-zA-Z0-9 ]", "" );
            line = line.toLowerCase();
            java.lang.String[] words = line.split( " " );
            System.out.println( "Edge: (2,4) => def: words, use: line, words" );
            if (words[0].equals( lastWord )) {
                System.out.println( "Edge: (4,5) => words[0].equals(lastWord)" );
                System.out.println( "Repeated word on line " + numberOfLines++ + ": " + words[0] );
            } else {
                System.out.println( "Edge: (2,5) => !words[0].equals(lastWord)" );
            }
            System.out.println( "Edge: (5,6) => def: i, use: i, words" );
            for (int i = 0; i < words.length - 1; i++) {
                System.out.println( "Edge: (6,7) => i < (words.length-1)" );
                if (words[i].equals( words[i + 1] )) {
                    System.out.println( "Edge: (7,9) => words[i].equals(words[i+1]" );
                    System.out.println( "Repeated word on line " + numberOfLines + ": " + words[i] );
                    System.out.println( "Edge: (9,10) => print \"Repeated word on line \"" );
                } else {
                    System.out.println( "Edge: (7,10) => i >= (words.length-1)" );
                }
                System.out.println( "Edge: (10,6) => use: i++" );
            }
            lastWord = words[words.length - 1];
            line = input.readLine();
            numberOfLines++;
            System.out.println( "Edge: (8,1) => i >= (words.length-1)" );
        }
        System.out.println( "Edge: (1,3) => line == null" );
    }

}
